#!/bin/sh
echo 1qaz2wsx |sudo -v
# 检查是否已配置 8.8.8.8 为 DNS
grep -q "nameserver 8.8.8.8" /etc/resolv.conf
# 如果没有找到 8.8.8.8，添加它
if [ $? -ne 0 ]; then
  echo "nameserver 8.8.8.8" | sudo tee -a /etc/resolv.conf > /dev/null
fi
ping -c 1 gitee.com &>/dev/null
# 如果网络不可用，显示红色警告信息并退出
if [ $? -ne 0 ]; then
  echo -e "\033[31mWarning: network is not available, please check your network settings！\033[0m"
  sleep 1s
  exit 1
fi
# 如果网络正常，继续执行后续操作
echo -e "network is ok, continue to update software...\n"
# 删除 platform_bak 目录
echo 1qaz2wsx | sudo -S rm -rf /home/feibot/platform_bak
# 重命名 platform 为 platform_bak
mv /home/feibot/platform /home/feibot/platform_bak
# 克隆仓库
cd /home/feibot
git clone https://gitee.com/feibot/f800-linux-python.git /home/feibot/platform
git config --global credential.helper store
# 403 错误也能正确返回
#todo: 增加判断，判断返回码——》200/203

# 检查是否克隆失败
if [ $? -ne 0 ]; then
  echo -e "\033[31mClone failed,Original version restored\033[0m"
  mv /home/feibot/platform_bak /home/feibot/platform
fi
sleep 1s
# 继续执行后续操作，无论克隆是否成功
cd /home/feibot/platform
git checkout origin/zzz
sleep 1s
chmod 777 -R /home/feibot/platform
sleep 1s

